package com.ctshk.rpc.order.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.tour.code.IsDeletedCode;
import com.ctshk.rpc.order.tour.dto.SeriesOrderTouristCertificateDTO;
import com.ctshk.rpc.order.tour.entity.SeriesOrderTouristCertificate;
import com.ctshk.rpc.order.tour.mapper.SeriesOrderTouristCertificateMapper;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristCertificateAddReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristCertificateDeleteReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristCertificateListReq;
import com.ctshk.rpc.order.tour.service.ISeriesOrderTouristCertificateService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系列团证件文件 服务实现类
 * </p>
 *
 * @author 谭响
 * @since 2021-01-21
 */
@DubboService
public class SeriesOrderTouristCertificateServiceImpl extends ServiceImpl<SeriesOrderTouristCertificateMapper, SeriesOrderTouristCertificate> implements ISeriesOrderTouristCertificateService {

    @Autowired
    private SeriesOrderTouristCertificateMapper seriesOrderTouristCertificateMapper;

    /**
     * 添加
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(SeriesOrderTouristCertificateAddReq req) {
        SeriesOrderTouristCertificate certificate  = EntityUtil.copy(req, SeriesOrderTouristCertificate.class);
        LocalDateTime now = LocalDateTime.now();
        certificate.setGmtCreate(now);
        certificate.setGmtModified(now);
        certificate.setCreateId(req.getUserId());
        certificate.setModifiedId(req.getUserId());
        certificate.setIsDeleted(IsDeletedCode.NO.getCode());
        save(certificate);
        return Result.success();
    }

    /**
     * 列表
     */
    @Override
    public Result<List<SeriesOrderTouristCertificateDTO>> list(SeriesOrderTouristCertificateListReq req) {
        QueryWrapper<SeriesOrderTouristCertificate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SeriesOrderTouristCertificate::getTouristId, req.getTouristId());
        queryWrapper.lambda().eq(SeriesOrderTouristCertificate::getCertificateType,req.getCertificateType());
        queryWrapper.lambda().eq(SeriesOrderTouristCertificate::getIsDeleted,IsDeletedCode.NO.getCode());
        List<SeriesOrderTouristCertificate> certificateList = seriesOrderTouristCertificateMapper.selectList(queryWrapper);
        List<SeriesOrderTouristCertificateDTO> certificateDTOList = EntityUtil.copyList(certificateList, SeriesOrderTouristCertificateDTO.class);
        return Result.success(certificateDTOList);
    }

    /**
     * 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(SeriesOrderTouristCertificateDeleteReq req) {
        SeriesOrderTouristCertificate certificate = EntityUtil.copy(req, SeriesOrderTouristCertificate.class);
        certificate.setIsDeleted(IsDeletedCode.YES.getCode());
        updateById(certificate);
        return Result.success();
    }
}
